package com.jzx.provider.ucenter.user.controller;

import com.jzx.provider.common.result.InvokeResult;
import com.jzx.provider.ucenter.user.dto.LoginCommand;
import com.jzx.provider.ucenter.user.dto.UserCommand;
import com.jzx.provider.ucenter.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Pattern;

/**
 * @author daihuihui
 * @date 2019/2/7
 */
@Validated
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;


    @PostMapping("/test")
    public InvokeResult<Void> test() {
        System.out.println("123");
        return InvokeResult.success();
    }

    @PostMapping("/save")
    public InvokeResult<Void> save(@RequestBody @Validated UserCommand userCommand) {
        userService.save(userCommand);
        return InvokeResult.success();
    }

    @GetMapping("/sendCode/{mobile}")
    public InvokeResult<Void> sendCode(@PathVariable @Pattern(regexp = "^1\\d{10}$") String mobile) {
        userService.sendCode(mobile);
        return InvokeResult.success();
    }

    @PostMapping("/login")
    public InvokeResult<String> login(@RequestBody @Validated LoginCommand loginCommand) {
        return InvokeResult.success(userService.login(loginCommand));
    }
}
